package com.api.client;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.scribe.builder.api.Api;
import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.OAuthConfig;
import org.scribe.model.OAuthRequest;
import org.scribe.model.SignatureType;
import org.scribe.model.Token;
import org.scribe.oauth.OAuthService;

public class OAuthSigner {

	private Log logger = LogFactory.getLog(getClass());

	private static final Api DEFAULT_API = new DefaultApi10a() {
		@Override
		public String getRequestTokenEndpoint() {
			return null;
		}

		@Override
		public String getAccessTokenEndpoint() {
			return null;
		}

		@Override
		public String getAuthorizationUrl(Token requestToken) {
			return null;
		}
	};

	private final OAuthService service;

	private final Token consumerToken;

	public OAuthSigner(String consumerKey, String consumerSecret) {

		if (consumerKey == null || consumerSecret == null) {

			throw new IllegalArgumentException("Consumer tokens cannot be null");
		}
		this.consumerToken = new Token(consumerKey, consumerSecret);
		this.service = DEFAULT_API.createService(new OAuthConfig(consumerToken.getToken(), consumerToken.getSecret(), "oob", SignatureType.Header, null, null));
	}

	public void sign(OAuthRequest request, String accessKey, String accessSecret) {

		Token accessToken = new Token(accessKey, accessSecret);

		service.signRequest(accessToken, request);

		logger.info(String.format("Request signed with AccessKeys: ['%s', '%s'] - CosumerKeys: ['%s' , '%s']", accessKey, accessSecret, consumerToken.getToken(), consumerToken.getSecret()));
	}
}
